using System.Text;
using System.Xml;

namespace f1__文件浏览器
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            treeView1.AfterSelect += treeView1_AfterSelect;
            treeView1.BeforeExpand += treeView1_BeforeExpand;
            listView1.DoubleClick += listView1_DoubleClick;
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void LoadRootDirectory()
        {
            string userFolder = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
            TreeNode rootNode = new TreeNode(userFolder) { Tag = userFolder };
            treeView1.Nodes.Add(rootNode);
            LoadSubDirectories(rootNode);
        }

        private void LoadSubDirectories(TreeNode parentNode)
        {
            string path = parentNode.Tag.ToString();
            try
            {
                foreach (var dir in Directory.GetDirectories(path))
                {
                    TreeNode node = new TreeNode(Path.GetFileName(dir)) { Tag = dir };
                    parentNode.Nodes.Add(node);
                }
            }
            catch { /* 忽略权限异常 */ }
        }

        private void treeView1_BeforeExpand(object sender, TreeViewCancelEventArgs e)
        {
            if (e.Node.Nodes.Count == 0) return;
            e.Node.Nodes.Clear();
            LoadSubDirectories(e.Node);
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            string path = e.Node.Tag.ToString();
            listView1.Items.Clear();

            try
            {
                // 显示子文件夹
                foreach (var dir in Directory.GetDirectories(path))
                {
                    var item = new ListViewItem(Path.GetFileName(dir));
                    item.SubItems.Add("文件夹");
                    item.Tag = dir;
                    listView1.Items.Add(item);
                }

                // 显示文件
                foreach (var file in Directory.GetFiles(path))
                {
                    var fi = new FileInfo(file);
                    var item = new ListViewItem(fi.Name);
                    item.SubItems.Add(fi.Extension);
                    item.SubItems.Add((fi.Length / 1024).ToString() + " KB");
                    item.Tag = file;
                    listView1.Items.Add(item);
                }
            }
            catch { }
        }

        private void listView1_DoubleClick(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count == 0) return;

            string path = listView1.SelectedItems[0].Tag.ToString();

            if (Path.GetExtension(path).ToLower() == ".xml")
            {
                ShowXmlContent(path);
            }
        }

        private void ShowXmlContent(string path)
        {
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(path);
                StringBuilder sb = new StringBuilder();
                ParseXmlNode(doc.DocumentElement, sb, 0);

                MessageBox.Show(sb.ToString(), "XML 内容");
            }
            catch (Exception ex)
            {
                MessageBox.Show("无法解析 XML：" + ex.Message);
            }
        }

        private void ParseXmlNode(XmlNode node, StringBuilder sb, int indent)
        {
            string indentStr = new string(' ', indent * 2);
            sb.AppendLine($"{indentStr}元素：<{node.Name}>");

            if (node.Attributes != null)
            {
                foreach (XmlAttribute attr in node.Attributes)
                {
                    sb.AppendLine($"{indentStr}  属性：{attr.Name} = \"{attr.Value}\"");
                }
            }

            if (!string.IsNullOrWhiteSpace(node.InnerText) && node.ChildNodes.Count == 1)
            {
                sb.AppendLine($"{indentStr}  文本：{node.InnerText.Trim()}");
            }

            foreach (XmlNode child in node.ChildNodes)
            {
                if (child.NodeType == XmlNodeType.Element)
                {
                    ParseXmlNode(child, sb, indent + 1);
                }
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            listView1.View = View.Details;

            LoadRootDirectory();

            
        }
    }
}
